package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import basic.Player;

import kontroler.Kontroler;

@SuppressWarnings("serial")
public class SouthPanel extends JScrollPane{

	private JTable table = null;
	private DefaultTableModel tableModel = null;
	private CenterPanel centerPanel = null;
	private Kontroler kontroler = null;
	private JPopupMenu popUpMenu = null;
	private JMenuItem addPlayer = null;
	private JMenuItem removePlayer = null;
	private JMenuItem editPlayer = null;
	private JMenuItem close = null;
	private PlayerDialog playerDialog = null;
	
	public SouthPanel(CenterPanel centerPanel) {
		super();
		this.centerPanel = centerPanel;
		this.kontroler = centerPanel.getKontroler();
		initialize();
	}
	
	private void initialize(){
		this.setPreferredSize(new Dimension(300,200));
		this.setViewportView(getTable());
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				if (e.getButton() == 3){
					getPopUpMenu().setVisible(true);
					getPopUpMenu().show(null, e.getXOnScreen(), e.getYOnScreen());
				}
			}
		});
		//getPlayerDialog();
	}

	public JTable getTable() {
		if (table == null){
			table = new JTable(getTableModel());
			table.setFont(new Font("Algerian", Font.PLAIN, 14));
			JTableHeader header = table.getTableHeader();
			header.setBackground(Color.lightGray);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					super.mouseReleased(e);
					if (e.getButton() == 3){
						getPopUpMenu().setVisible(true);
						getPopUpMenu().show(null, e.getXOnScreen(), e.getYOnScreen());
					}
				}
			});
			
		}
		return table;
	}

	public DefaultTableModel getTableModel() {
		if (tableModel == null){
			tableModel = new DefaultTableModel();
			tableModel.addColumn("Name");
			tableModel.addColumn("Years");
			tableModel.addColumn("Role");
			tableModel.addColumn("Games played");
			tableModel.addColumn("Goals");
			tableModel.addColumn("Assists");
			tableModel.addColumn("Shots on");
			tableModel.addColumn("Shots off");
			tableModel.addColumn("Yellow cards");
			tableModel.addColumn("Red cards");
		}
		return tableModel;
	}

	public JPopupMenu getPopUpMenu() {
		if (popUpMenu == null){
			popUpMenu = new JPopupMenu();
			popUpMenu.add(getAddPlayer());
			popUpMenu.add(getEditPlayer());
			popUpMenu.add(getRemovePlayer());
			popUpMenu.addSeparator();
			popUpMenu.add(getClose());
		}
		return popUpMenu;
	}

	public JMenuItem getAddPlayer() {
		if (addPlayer == null){
			addPlayer = new JMenuItem("Add Player");
			addPlayer.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					popUpMenu.setVisible(false);
					getPlayerDialog().setVisible(true);
				}
			});
		}
		return addPlayer;
	}

	public JMenuItem getRemovePlayer() {
		if (removePlayer == null){
			removePlayer = new JMenuItem("Remove player");
		}
		return removePlayer;
	}

	public JMenuItem getEditPlayer() {
		if (editPlayer == null){
			editPlayer = new JMenuItem("Edit player");
		}
		return editPlayer;
	}

	public JMenuItem getClose() {
		if (close == null){
			close = new JMenuItem("Close");
			close.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					popUpMenu.setVisible(false);
				}
			});
		}
		return close;
	}
	
	public Kontroler getKontroler(){
		return kontroler;
	}
	
	public CenterPanel getCenterPanel(){
		return centerPanel;
	}
	
	private PlayerDialog getPlayerDialog(){
		if (playerDialog == null){
			playerDialog = new PlayerDialog(this);
		}
		return playerDialog;
	}
	
	public void populateTable(){
		for (int j = tableModel.getRowCount()-1;j >= 0;j--){
			tableModel.removeRow(j);
		}
		LinkedList<Player> players = centerPanel.getSelectedClub().getPlayers();
		for(int i = 0; i < players.size(); i++){
			tableModel.addRow(players.get(i).getPlayerView());
		}
	}
	
	
	
	
}
